package com.yykj.tencent.im.server.controller;

import com.yykj.tencent.im.server.bean.request.FriendGetRequest;
import com.yykj.tencent.im.server.bean.response.FriendGetResult;
import com.yykj.tencent.im.server.bean.response.UserDataItem;
import com.yykj.tencent.im.server.dto.ContactDTO;
import com.yykj.tencent.im.server.service.FriendService;
import com.yykj.tencent.im.server.service.ProfileService;
import com.yykj.tencent.im.server.service.SnsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "腾讯云通信--好友管理")
@RequestMapping("api/tencent/im/friend")
public class FriendController {
    @Resource
    private FriendService friendService;
    @Resource
    private ProfileService profileService;
    @Resource
    private SnsService snsService;


    @ApiOperation("常用联系好友/团队")
    @GetMapping("/contact")
    public List<ContactDTO> contact(@RequestParam(name = "fromAccount") String fromAccount){
         return friendService.contact(fromAccount);
    }

    @ApiOperation("我的好友")
    @GetMapping("/me")
    public List<ContactDTO> me(@RequestParam(name = "fromAccount") String fromAccount){
        //好友的ID
        List<String> accounts = new ArrayList<>();
        FriendGetResult result;
        FriendGetRequest friendGetRequest;
        Integer startIndex = 0;
        do{
            friendGetRequest = FriendGetRequest.builder()
                    .fromAccount(fromAccount)
                    .startIndex(startIndex)
                    .build();
            result = snsService.friendGet(friendGetRequest);
            List<UserDataItem> items = result.getUserDataItemList();
            if(items!=null && items.size()>0){
                for(UserDataItem item:items){
                    accounts.add(item.getToAccount());
                }
                startIndex = result.getNextStartIndex();
            }else{
                break;
            }

        }while(!(result.getCompleteFlag() > 0));
        if(accounts.size() == 0){
            return new ArrayList<>();
        }
        return profileService.getFriendProfile(accounts);
    }




}
